// pages/personal-center/invoice.js
var app = getApp();
var api = require('../../api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    start:'',
    tax_id:'',
    address: '',
    tel: '',
    bank: '',
    bank_card: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.show_my_invoice()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  show_my_invoice(){
    var that=this
    app.request({
      url: api.user.showmyinvoice,
      method: 'POST',
      data: {},
      success: function (resp) {
        if (resp.errcode == 0) {
          that.setData({
            start: resp.data.start,
            tax_id: resp.data.tax_id,
            address: resp.data.address,
            tel: resp.data.tel,
            bank: resp.data.bank,
            bank_card: resp.data.bank_card,
          })
        }
      },
      fail: function (resp) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },

  formSubmit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var address = e.detail.value.address
    var bank = e.detail.value.bank
    var bank_card = e.detail.value.bank_card
    var start = e.detail.value.start
    var tax_id = e.detail.value.tax_id
    var tel = e.detail.value.tel
    
    wx.showLoading({
      title: '正在提交',
    })
    app.request({
      url: api.user.invoice,
      method: 'POST',
      data: {
        address: address,
        bank: bank,
        bank_card: bank_card,
        start: start,
        tax_id: tax_id,
        tel: tel,
      },
      success: function (resp) {
        if (resp.errcode == 0) {
          wx.showToast({
            title: resp.data,
            duration: 1000
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        }
      },
      fail: function (resp) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  }

})